﻿#ifndef __SS_SOCKET_SELECT_H__
#define __SS_SOCKET_SELECT_H__
#include "ssNetBase.h"

namespace StarSeeker
{

class ssSocketSelect
{
public:
	// 构造和析构
	ssSocketSelect();
	ssSocketSelect(int sec, int usec);
	virtual ~ssSocketSelect();
	//主循环函数
	bool RunFrame(unsigned int ms);
	//设置为阻塞模式
	void SetBlock(bool isBlock);
protected:
	// select 模式下的事件虚函数
	virtual void BeforeFDSET(){return;}
	virtual void ProcessFDSET(){return;}
	virtual void AfterFDSET(){return;}

	// 处理 struct fd_set的函数
	inline int IsReadSet(SOCKET s)
	{
		return FD_ISSET(s, &m_fdRead);
	}
	inline int IsWriteSet(SOCKET s)
	{
		return FD_ISSET(s, &m_fdWrite);
	}
	inline int IsExceptSet(SOCKET s)
	{
		return FD_ISSET(s, &m_fdExcept);
	}
	inline void ClearFDSet()
	{
		FD_ZERO(&m_fdRead);
		FD_ZERO(&m_fdWrite);
		FD_ZERO(&m_fdExcept);
	}
	inline void AppendReadFDSet(SOCKET s)
	{
        FD_SET(s, &m_fdRead);
	}
	inline void AppendWriteFDSet(SOCKET s)
	{
        FD_SET(s, &m_fdWrite);
	}
	inline void AppendExceptFDSet(SOCKET s)
	{
        FD_SET(s, &m_fdExcept);
	}

	// fd_set
	fd_set			m_fdRead;
	fd_set			m_fdWrite;
	fd_set			m_fdExcept;
	// select timeout time
    timeval			m_timeout;
    SOCKET          m_nfds;

	bool			m_enable;
	unsigned int	m_frameTime;
	bool			m_isBlock;
};

}
#endif
